package controller

import (
	"dks-goweb/model"
	"dks-goweb/service"
	"dks-goweb/utils"
	"fmt"
	"net/http"

	"github.com/gin-gonic/gin"
)

// QueryCardStatus 处理查询卡片状态的请求
func QueryCardStatus(c *gin.Context) {
	var req model.CardRequest
	//读取问号参数
	req.Card = c.Query("card")

	// 调用service层的查询函数
	response, err := service.QueryCardStatus(req.Card)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	// 返回自定义的响应数据
	c.JSON(http.StatusOK, response)
}

// QueryCardPackage处理网忆卡套餐信息
func QueryCardPackage(c *gin.Context) {
	reqCard := c.Query("card")
	responseBody, err := service.QueryCardPackage(reqCard)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
	}

	// 返回响应给客户端
	c.JSON(http.StatusOK, responseBody)
}

func QueryFlowPool(c *gin.Context) {
	reqCard := c.Query("card")
	tokens, err := service.GetAccessToken(reqCard)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
	}
	fmt.Println(tokens)
	simplifiedResponse, err := service.QueryPoolPackageInfo(tokens, reqCard)
	if err != nil {

		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
	}
	c.JSON(http.StatusOK, simplifiedResponse)

}

// 预留给三个平台的接口
func QueryCardStatusAndPackage(c *gin.Context) {
	reqCard := c.Query("card")
	//去除字符空格和删除最后一个字母
	reqCard = utils.TrimSpacesAndRemoveLastLetter(reqCard)
	wyCard, err := service.QueryCardStatusAndPackage(reqCard)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error1": err.Error()})
		return
	}
	//判断是不是网亿卡
	if wyCard.CardDealer == "未知" {

		token, err := service.GetAccessToken(reqCard)
		fmt.Print(token + "\n")
		fmt.Print(reqCard + "\n")
		if err != nil {
			c.JSON(http.StatusInternalServerError, gin.H{"error2": err.Error()})
			return
		}
		poolPackage, err := service.QueryPoolPackageInfo(token, reqCard)
		//判断是不是网亿流量池卡
		if poolPackage.CardDealer == "未知" {

			zdPool, err := service.QueryCardZdPool(reqCard)
			//判断是不是中点卡
			if zdPool.CardDealer == "未知" {
				//如果不是中点卡就直接查询妙月卡不管是不是都返回
				myService, err := service.MYService(reqCard)
				if err != nil {
					c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
					return
				}
				c.JSON(http.StatusOK, myService)

			} else {
				if err != nil {
					c.JSON(http.StatusInternalServerError, gin.H{"error3": err.Error()})
					return
				}

				c.JSON(http.StatusOK, zdPool)

			}

		} else {
			c.JSON(http.StatusOK, poolPackage)
		}

	} else {
		c.JSON(http.StatusOK, wyCard)
	}
}

// 中点物联简化请求接口
func QueryCardZdPool(c *gin.Context) {
	cardNum := c.Query("card")
	responseBody, err := service.QueryCardZdPool(cardNum)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})

	}
	c.JSON(http.StatusOK, responseBody)
}

// 刷新普通网易卡
func RefreshController(c *gin.Context) {
	reqCard := c.Query("card")
	reqCard = utils.TrimSpacesAndRemoveLastLetter(reqCard)
	response := service.RefreshService(reqCard)
	c.JSON(http.StatusOK, response)
}

func MYcontroller(c *gin.Context) {
	reqCard := c.Query("card")
	myService, err := service.MYService(reqCard)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, myService)
}
